package com.vim.modules.web.service;

import com.vim.common.utils.IdGen;
import com.vim.common.base.CrudServiceImpl;
import com.vim.common.utils.WeixinUtils;
import com.vim.modules.web.model.WeixinUser;
import com.vim.modules.web.dao.WeixinUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WeixinUserService extends CrudServiceImpl<WeixinUser> {

    @Resource
    private WeixinUserMapper weixinUserMapper;

    @Autowired
    public WeixinUserService(WeixinUserMapper mapper) {
        super(mapper);
    }

    @Override
    public int save(WeixinUser entity) {
        entity.setUserId(IdGen.uuid());
        return super.save(entity);
    }

    /**
     * 初始化微信用户列表
     */
    @Transactional(rollbackFor = Exception.class)
    public void initList() {
        List<WeixinUser> userList = WeixinUtils.userlist("");
        for(WeixinUser user:userList){
            save(user);
        }
    }
}

